import type { App, Component } from 'vue'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'

type componentsObj = {
  [index: string]: Component
}

export default function (app: App) {
  // 这是webpack中的写法，在vite中不适用
  // const files = window.require.context('@/components', true, /(\.ts)$/)
  const modules = import.meta.glob('@/components/**/index.ts', { eager: true }) // eager 是否是直接引入，控制懒加载import,默认false
  Object.keys(modules).forEach((filePath) => {
    const obj = modules[filePath] as componentsObj
    Object.keys(obj).forEach((component) => {
      app.component(component, obj[component])
    })
  })
  // 注册element图标组件
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
}
